﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CustomControl
{
	/// <summary>
	/// Interaction logic for ThenPage.xaml
	/// </summary>
	public partial class ThenPage : Page, INotifyPropertyChanged
	{
		public ThenPage()
		{

			InitializeComponent();
			ThenItems = new ObservableCollection<ThenItem>();
			ThenItems.Add(new ThenItem() { Title = "Send TCP Message", Icon = "Network.png"});
			ThenItems.Add(new ThenItem() { Title = "Send UDP Message", Icon = "Network.png"});
			ThenItems.Add(new ThenItem() { Title = "Send Serial Message", Icon = "Serial.png"});
			ThenItems.Add(new ThenItem() { Title = "Send WebAPI Message", Icon = "CloudRequest.png" });
			ThenItems.Add(new ThenItem() { Title = "Turn On Samsung TV", Icon = "Serial.png" });
			ThenItems.Add(new ThenItem() { Title = "Dim Lights", Icon = "GPIO.png" });
			ThenItems.Add(new ThenItem() { Title = "Send Log To Azure", Icon = "CloudRequest.png" });
			ThenItems.Add(new ThenItem() { Title = "Email Admin", Icon = "CloudRequest.png" });

			FilteredThenItems = CollectionViewSource.GetDefaultView(ThenItems);
			FilteredThenItems.SortDescriptions.Add(new SortDescription("Title", ListSortDirection.Ascending));

			this.DataContext = this;
		}

		string _searchText;
		public string SearchText
		{
			get
			{
				return _searchText;
			}
			set
			{
				if (_searchText != value)
				{
					_searchText = value;
					NotifyOfPropertyChanged("SearchText");
					ListCollectionView list = (ListCollectionView)FilteredThenItems;
					list.Filter = (obj) => {
						ThenItem t = obj as ThenItem;
						if (t != null)
						{
							return t.Title.IndexOf(_searchText, StringComparison.OrdinalIgnoreCase) >= 0;
						}
						else
						{
							return false;
						}
					};
				}
			}
		}


		ICollectionView _filteredThenItems;
		public ICollectionView FilteredThenItems
		{
			get
			{
				return _filteredThenItems;
			}
			set
			{
				if (_filteredThenItems != value)
				{
					_filteredThenItems = value;
					NotifyOfPropertyChanged("FilteredThenItems");
				}
			}
		}


		public event PropertyChangedEventHandler PropertyChanged;

		ObservableCollection<ThenItem> _thenItems;
		public ObservableCollection<ThenItem> ThenItems
		{
			get
			{
				return _thenItems;
			}
			set
			{
				if (_thenItems != value)
				{
					_thenItems = value;
					NotifyOfPropertyChanged("ThenItems");
				}
			}
		}



		void NotifyOfPropertyChanged(string prop)
		{
			if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
		}

			

	}

	public class ThenItem : INotifyPropertyChanged
	{
		string _Title;
		public string Title
		{
			get
			{
				return _Title;
			}
			set
			{
				if (_Title != value)
				{
					_Title = value;
					NotifyOfPropertyChanged("Title");
				}
			}
		}

		string _Icon;
		public string Icon
		{
			get
			{
				return _Icon;
			}
			set
			{
				if (_Icon != value)
				{
					_Icon = value;
					NotifyOfPropertyChanged("Icon");
				}
			}
		}


		public event PropertyChangedEventHandler PropertyChanged;

		void NotifyOfPropertyChanged(string prop)
		{
			if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
		}

			
	}

	public class ImagePathConverter : IValueConverter
	{


		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return "pack://application:,,,/Images/" + value.ToString();
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
